import NProgress from "nprogress";
import service from "@/base/axios";
import {isAxiosError} from "axios";


interface CumRequest {
    get(url: string, params?: any): Promise<any>

    post(url: string, params?: any): Promise<any>

    upload(url: string, params: any): Promise<any>
}

/**
 * 定义路由请求接口
 */
class RequestManager implements CumRequest {
    /**
     * get请求
     *
     * @param url
     * @param params
     */
    get(url: string, params?: unknown): Promise<any> {
        return new Promise((resolve, reject) => {
            NProgress.start()
            service.get(url, {params: params})
                .then((res) => {
                    NProgress.done()
                    resolve(res)
                })
                .catch((err) => {
                    NProgress.done()
                    reject(err)
                })
        })
    }

    down(url: string): Promise<void> {
        return new Promise((resolve, reject) => {
            service.get(url, {  // 改为 GET 请求
                responseType: 'blob',
                headers: {}
            })
                .then((res) => {
                    const blob = new Blob([res.data], {
                        type: res.headers['content-type']  // 使用响应头中的 Content-Type
                    });
                    const objectUrl = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');

                    // 从响应头中提取文件名
                    const disposition = res.headers['content-disposition'];
                    let fileName = '数据导入-版本一.xls';
                    if (disposition) {
                        const matches = /filename\*=UTF-8''([^;]+)/.exec(disposition);
                        if (matches && matches[1]) {
                            fileName = decodeURIComponent(matches[1]);
                        }
                    }

                    a.href = objectUrl;
                    a.download = fileName;
                    a.style.display = 'none';
                    document.body.appendChild(a);
                    a.click();  // 触发下载
                    document.body.removeChild(a);
                    window.URL.revokeObjectURL(objectUrl);
                    resolve();
                })
                .catch((err) => {
                    console.error('下载失败:', err);
                    reject(err);
                });
        });
    }
    /**
     * post 请求
     * @param url
     * @param params
     */
    post(url: string, params ?: unknown):
        Promise<any> {
        return new Promise((resolve, reject) => {
            // 进度条
            NProgress.start()
            service.post(url, params)
                .then((res) => {
                    NProgress.done()
                    resolve(res)
                })
                .catch((err) => {
                    NProgress.done()
                    reject(err)
                })
        })
    }


    /**
     * 文件上传
     * @param url
     * @param file
     */
    upload(url: string, file: any):
        Promise<any> {
        return new Promise((resolve, reject) => {
            NProgress.start()
            service
                .post(url, file, {
                    headers: {'Content-Type': 'multipart/form-data'}
                })
                .then((res) => {
                    NProgress.done()
                    resolve(res)
                })
                .catch((err) => {
                    NProgress.done()
                    reject(err)
                })
        })
    }

}

const
    request = new RequestManager()
export default request
